<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Type extends Model
{
    use  SoftDeletes;
    protected $table = 'types';
    protected $fillable = [
        'title'
    ];

    /**
     * 添加分类
     * @param $data
     * @return bool
     */
    static public function add($data)
    {
        $re = self::create($data);
        if ($re) {
            return true;
        }
        return false;
    }

    /**
     * 编辑
     * @param $id
     * @param $title
     * @return bool
     */
    static public function edit($id, $title)
    {
        $type = self::find($id);
        $type->title = $title;
        if ($type->save())
        {
            return true;
        }
        return false;
    }

    /**
     * 搜索
     * @param $query
     * @param $title
     */
    public function  scopeTitle($query,$title)
    {
        if($query){
            $query->where('title','like','%'.$title.'%');
        }

    }
}
